/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    loadList('/v1/teacher/apart/list.json');
    $('#view-apply-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        var region_name = button.data('region-name');
        var type = button.data('type');
        var area = button.data('area');
        $(this).find('.apart-id').val(id);
        $(this).find('.apart-name').text(name);
        $(this).find('.region-name').text(region_name);
        var apart_type = '';
        switch (type) {
            case 1 : apart_type = '一居室';break;
            case 2 : apart_type = '一室一厅';break;
            case 3 : apart_type = '两室一厅';break;
            case 4 : apart_type = '三室一厅';break;
            case 5 : apart_type = '三室两厅';break;
            default: apart_type =  '一居室';break;
        }
        $(this).find('.apart-type').text(apart_type);
        $(this).find('.apart-area').text(area);
    });
    $('#view-apply-dialog').on('hide.bs.modal', function(){
        $(this).find('.apart-id').val('');
        $(this).find('.apart-name').text('');
        $(this).find('.region-name').text('');
        $(this).find('.apart-type').text('');
        $(this).find('.apart-area').text('');
    });
    $('#view-apply-dialog .btn-save').on('click', function(){
        var id = $('#view-apply-dialog .apart-id').val();
        sendAjax('/v1/teacher/apart/apply', 'post', {id:id}, function(json){
            $('#view-apply-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url) {
    $("#apart-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'region_name',
            title: '所在地'
        }, {
            field: 'type',
            title: '类型',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '一居室';
                    case 2 : return '一室一厅';
                    case 3 : return '两室一厅';
                    case 4 : return '三室一厅';
                    case 5 : return '三室两厅';
                    default : return '一居室';
                }
            }
        }, {
            field: 'area',
            title: '面积'
        }, {
            field: 'rent',
            title: '月租金',
            formatter: function (value) {
                return value + '元';
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                return '<a class="btn btn-xs btn-primary apply-apart" data-toggle="modal" data-target="#view-apply-dialog" data-id="'+row.id+'" data-name="'+row.name+'" data-region-name="'+row.region_name+'" data-type="'+row.type+'" data-area="'+row.area+'">申请</a>';
            }
        }]
    });
}
